<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CourseClass;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseClassController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CourseClass(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('name')->tree();
            $grid->column('img')->image(' ', 50);
            $grid->column('sort');
            $grid->column('status')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseClass(), function (Show $show) {
            $show->field('id');
            $show->field('pid');
            $show->field('name');
            $show->field('img');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseClass(), function (Form $form) {
            $form->display('id');
            $form->select('pid', '顶级分类')->options(([0 => '顶级分类'] + \App\Models\CourseClass::where('status', 1)->where('pid', 0)->pluck('name', 'id')->all()))->default(0);
            $form->text('name')->required();
            $form->image('img')->uniqueName()->autoUpload()->saveFullUrl();
            $form->number('sort')->default(0);
            $form->radio('status')->options(['禁用', '正常'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
